;(function($){
	$.open_table_list = $.open_table_list || {};
	$.fn.open_table_list = function(opts){
		var el = this;
		var opt = {
			unOpenArr : [],
			ct_brzdArr : [],
			ktxfArr : [],
			ktjsArr : [],
			depositArr : [],
			rmpackage : [],
			jdInfo : {},
			selTabelOper : function(){},
			dataBack : function(){},
			closeBack : function(){},
			showShouPai : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
		$.open_table_list = $.extend($.open_table_list,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			
			var el = this.el;
			var param = this.param;
			var t = this;
			
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var html = t.initHtml_mainInfo() + t.initHtml_mainList() + t.initHtml_mainFooter();
			el.html(html);
			t.setSize();
		},
		initHtml_mainInfo : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = 	'<div id="mainInfo" class="widget-box row">'+
			
							'<div class="col-sm-12">'+
								'<div class="profile-user-info">'+
									'<div class="profile-info-row">'+
										'<div class="profile-info-name" data-i18n="备注">'+$.i18n.propJsp("接待号")+'</div>'+
										'<div class="profile-info-value">'+
											'<span id="jdh"></span>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="col-sm-6">'+
								'<div class="profile-user-info">'+
									'<div class="profile-info-row">'+
										'<div class="profile-info-name" data-i18n="公司">'+$.i18n.propJsp("公司")+'</div>'+
										'<div class="profile-info-value">'+
											'<span id="company"></span>'+
											'<span id="pno" class="hide"></span>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="col-sm-6">'+
								'<div class="profile-user-info">'+
									'<div class="profile-info-row">'+
										'<div class="profile-info-name" data-i18n="市场">'+$.i18n.propJsp("市场")+'</div>'+
										'<div class="profile-info-value">'+
											'<span id="market"></span>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="col-sm-12">'+
								'<div class="profile-user-info">'+
									'<div class="profile-info-row">'+
										'<div class="profile-info-name" data-i18n="备注">'+$.i18n.propJsp("备注")+'</div>'+
										'<div class="profile-info-value">'+
											'<span id="remark"></span>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			return html;
		},
		initHtml_mainList : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = '<div id="jdInfoList" class="widget-box jdInfoList no-margin" style="display:flex;">'+
							'<div id="jdInfoGridWidth" class="widget-box jdInfoList-grid no-margin">'+
								'<div id="" class="widget-body">'+
									'<div id="selShoupaiList" class="widget-main no-padding"></div>'+
								'</div>'+
							'</div>'+
							'<div class="widget-box jdInfoList-oper no-margin">'+
								'<table style="width:100% !important;">'+
									'<tbody>'+
										'<tr>'+
											'<td>'+
												'<table class="btnContainer" style="width:100%;"><tbody>'+
													'<tr><td>'+
														'<button class="btn btn-app btn-grey btn-xs oper_menu" data-oper="setSuper_flag" style="margin: 0px !important;">'+
															$.i18n.propJsp("主牌")+
														'</button>'+
													'</td></tr>'+
													'<tr><td>'+
														'<button class="btn btn-app btn-grey btn-xs oper_menu" data-oper="del" style="margin: 0px !important;">'+
															$.i18n.propJsp("删除")+
														'</button>'+
													'</td></tr>'+
													'<tr><td>'+
														'<button class="btn btn-app btn-grey btn-xs oper_menu" data-oper="积分卡" style="margin: 0px !important;">'+
															$.i18n.propJsp("积分卡")+
														'</button>'+
													'</td></tr>'+
													'<tr><td>'+
														'<button class="btn btn-app btn-grey btn-xs oper_menu" data-oper="刷门票" style="margin: 0px !important;">'+
															$.i18n.propJsp("刷门票")+
														'</button>'+
													'</td></tr>'+
												'</tbody></table>'+
											'</td>'+
										'</tr>'+
									'</tbody>'+
								'</table>'+
							'</div>'+
						'</div>'
			return html;
		},
		initHtml_mainFooter : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = 	'<div class="widget-box open-table-option-amount" style="height:63px;">'+//padding-top:18px;
							'<div class="open-table-option-amount-header"></div>'+//'+$.i18n.propJsp("合计：")+'
							'<div class="open-table-option-amount-content">'+
								'<div class="open-table-opetion-amount-num">'+
									'<div class="open-table-opetion-amount-item">'+
										'<font>'+$.i18n.propJsp("男:")+'</font>'+
										'<span class="item-total num-item-m">0</span>'+
									'</div>'+
									'<div class="open-table-opetion-amount-item">'+
										'<font>'+$.i18n.propJsp("女:")+'</font>'+
										'<span class="item-total num-item-w">0</span>'+
									'</div>'+
									'<div class="open-table-opetion-amount-item">'+
										'<font>'+$.i18n.propJsp("合:")+'</font>'+
										'<span class="item-total num-item-total">0</span>'+
									'</div>'+
								'</div>'+
								'<div class="open-table-opetion-amount-money">'+
									'<div class="open-table-opetion-amount-item">'+
										'<font>'+
											'<i data-icon="deposit" class="ace-icon fa fa-rmb icon-deposit"></i>'+
											$.i18n.propJsp("押金:")+
										'</font>'+
										'<span class="item-total money-item-deposit">0</span>'+
									'</div>'+
									'<div class="open-table-opetion-amount-item">'+
										'<font>'+
											'<i data-icon="ktxf" class="ace-icon fa fa-shopping-cart icon-ktxf"></i>'+
											$.i18n.propJsp("消费:")+
										'</font>'+
										'<span class="item-total money-item-ktxf">0</span>'+
									'</div>'+
									'<div class="open-table-opetion-amount-item">'+
										'<font>'+$.i18n.propJsp("合:")+'</font>'+
										'<span class="item-total money-item-total">0</span>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			
			var html = 	'<div class="row widget-box">'+
							'<div class="col-sm-12">'+
								
								'<div class="col-sm-4 amount-column">'+
								
									'<div class="profile-user-info">'+
										'<div class="profile-info-row">'+
											'<div class="profile-info-name">'+$.i18n.propJsp("男:")+'</div>'+
											'<div class="profile-info-value">'+
												'<span class="item-total num-item-m">0</span>'+
											'</div>'+
										'</div>'+
									'</div>'+
									
									'<div class="profile-user-info">'+
										'<div class="profile-info-row">'+
											'<div class="profile-info-name">'+$.i18n.propJsp("押金:")+'</div>'+
											'<div class="profile-info-value">'+
												'<span class="item-total money-item-deposit">0</span>'+
											'</div>'+
										'</div>'+
									'</div>'+
									
								'</div>'+
								
								'<div class="col-sm-4 amount-column">'+
									
									'<div class="profile-user-info">'+
										'<div class="profile-info-row">'+
											'<div class="profile-info-name">'+$.i18n.propJsp("女:")+'</div>'+
											'<div class="profile-info-value">'+
												'<span class="item-total num-item-w">0</span>'+
											'</div>'+
										'</div>'+
									'</div>'+
									
									'<div class="profile-user-info">'+
										'<div class="profile-info-row">'+
											'<div class="profile-info-name">'+$.i18n.propJsp("消费:")+'</div>'+
											'<div class="profile-info-value">'+
												'<span class="item-total money-item-ktxf">0</span>'+
											'</div>'+
										'</div>'+
									'</div>'+
									
								'</div>'+
								
								'<div class="col-sm-4 amount-column">'+
								
									'<div class="profile-user-info">'+
										'<div class="profile-info-row">'+
											'<div class="profile-info-name">'+$.i18n.propJsp("合:")+'</div>'+
											'<div class="profile-info-value">'+
												'<span class="item-total num-item-total">0</span>'+
											'</div>'+
										'</div>'+
									'</div>'+
									
									'<div class="profile-user-info">'+
										'<div class="profile-info-row">'+
											'<div class="profile-info-name">'+$.i18n.propJsp("合:")+'</div>'+
											'<div class="profile-info-value">'+
												'<span class="item-total money-item-total">0</span>'+
											'</div>'+
										'</div>'+
									'</div>'+
									
								'</div>'+
								
							'</div>'+
						'</div>';
			return html;
		},
		setSize : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var winH = $(window).height();
			var top = el.find("#jdInfoList").offset().top //+ 107;
			el.find("#mainInfo").css({height:"107"})
			var offset = parseFloat($(".page-content").css("padding-top")) //页面偏差
			var listH = winH - top - 78 + offset//68
			el.find("#jdInfoList").css({"height":listH-20});
			el.find("#selShoupaiList").css({"height":listH-20,"overflow-y":"auto"});
			var operH = parseInt(listH/7); 
			if(operH>60){
				operH = 60
			}
	 		el.find('.btnContainer').find('td').css({"height":operH})
	 		el.find('.btnContainer').find('button').css({"height":(operH-1.5)})
		},
		addUnopenShoupai : function(jsone,badgeJson,json1){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//添加到未提交
			t.editOpenShoupaiArr(jsone,"add");
			var index = el.find("#selShoupaiList").find(".each_undish").size()+1;
			var html='<div class="each_undish" id="'+jsone.bh+'" data-bh="'+jsone.bh+'">'+
						'<div class="btn btn-app btn-light no-radius dish_content current" data-index="'+index+'" data-id="tableView" style="background:'+jsone.bgcolor+' !important;">'+//背景颜色
							'<div class="jsone hide">'+ JSON.stringify(jsone) +'</div>'+
							'<div class="json hide">'+JSON.stringify(json1)+'</div>'+
							'<div class="each_top flex">'+
								'<div class="eachName flex">'+
									'<div class="dish_id">'+ index +'</div>.&nbsp;'+
									'<div data-name="'+jsone.mc+'" class="dish_name">'+ jsone.bh +'</div>&nbsp;'+
								'</div>'+
								'<div id="badgeHtml" class="eachRight flex">'+//添加项目
								'</div>'+
							'</div>'+ 
							'<div class="each_bottom flex">'+//做法备注
							'</div>'+
						'</div>'+
						'<div class="sub_dishes"></div>'+
					'</div>';
			el.find("#selShoupaiList").append(html);
			var badgeC = el.find("#selShoupaiList").find(".each_undish:last").find("#badgeHtml");
			t.initBadgeHtml(badgeJson,badgeC);
			t.setScrollPosition();
			t.bindShoupaiEvent();
		},
		initBadgeHtml : function(badgeJson,badgeC){
			var el = this.el;
			var param = this.param;
			var t = this;
			var json = $.parseJSON(badgeC.closest(".each_undish").find(".json").html());
			var badgeHtml = "";
			for(k in badgeJson){
				if(badgeJson[k]&&k!="dcode"){
					var cla = $("i[data-icon="+k+"]").attr("class")
					badgeHtml +='<span class="badge-'+k+'">'+
									'<i class="'+cla+'"></i>'+
									badgeJson[k]+
								'</span>';
				}
			}
			badgeC.html(badgeHtml);
		},
		bindShoupaiEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//选中手牌
			el.find("#selShoupaiList").find(".dish_content").unbind("click").click(function(){
				el.find("#selShoupaiList").find(".dish_content").removeClass("current");
				$(this).addClass("current");
			})
			el.find("#selShoupaiList").find(".dish_content:last").trigger("click");
			
			//中央按钮操作
			el.find(".oper_menu").unbind("click").click(function(e){
				var size = el.find("#selShoupaiList").find(".dish_content.current").size();
				var oper = $(this).data("oper")
				if(size==0){
					jf_successDelayed("请选择要操作的手牌!"); 
				}else{
					var $current = el.find("#selShoupaiList").find(".dish_content.current").eq(0); 
					var currentJson = $.parseJSON($current.find(".jsone").html());
					switch (oper) {
					case "setSuper_flag"://设置主牌
						el.find("#selShoupaiList").find(".super_flag").removeClass("super_flag")
						$current.find(".eachName").addClass("super_flag");
						break;
					case "del":
						var index = $current.data("index");
						t.editOpenShoupaiArr(parseInt(index),"del");
						var currentIndex = $current.data("index");
						$current.closest(".each_undish").remove();
						el.find(".each_undish").each(function(k,v){
							$(v).find(".dish_content").attr("data-index",k+1);
							$(v).find(".dish_id").html(k+1);
						})
						//设置选中
						var len = el.find(".dish_content").size();
						if(currentIndex>len){
							el.find(".dish_content:last").addClass("current");
						}else{
							el.find(".each_undish").find(".dish_content[data-index="+currentIndex+"]").addClass("current");
						}
						//删掉的手牌显示出来
						param.showShouPai(currentJson.bh);
						break;
					case "edit" :

						break;
					default:
						break;
					}
				};
			});
		},
		setScrollPosition : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
//			el.find("#selShoupaiList")[0].scrollHeight
			var h = el.find("#selShoupaiList").prop('scrollHeight');
			el.find("#selShoupaiList").scrollTop(h);
		},
		editOpenShoupaiArr : function(json,oper,index){
			var el = this.el;
			var param = this.param;
			var t = this;
			if(oper=="add"){
				param.unOpenArr.push(json);
				$.open_table_oper.param.unOpenArr.push(json);
				
				param.ct_brzdArr.push((json.th||"").toString());
				$.open_table_oper.param.ct_brzdArr.push((json.th||"").toString());
				
				param.ktxfArr.push((json.ktxf||"").toString());
				$.open_table_oper.param.ktxfArr.push((json.ktxf||"").toString());
				
				param.ktjsArr.push((json.ktjs||"").toString());
				$.open_table_oper.param.ktjsArr.push((json.ktjs||"").toString());
				
				var depositJson = {}
				depositJson[(json.dcode).toString()] = (json.deposit||"").toString();
				param.depositArr.push(depositJson);
				$.open_table_oper.param.depositArr.push(depositJson);
				
//				缺少客房
			}else if(oper=="init"){
				
				param.unOpenArr = [];
				$.open_table_oper.param.unOpenArr = [];
				
				param.ct_brzdArr = [];
				$.open_table_oper.param.ct_brzdArr = [];
				
				param.ktxfArr = [];
				$.open_table_oper.param.ktxfArr = [];
				
				param.ktjsArr = [];
				$.open_table_oper.param.ktjsArr = [];
				
				param.depositArr = [];
				$.open_table_oper.param.depositArr = [];
				
				t.setJdInfo({});
				
				el.find("#selShoupaiList").empty();
				el.find(".open-table-option-amount-content").find("spap.item-total").text(0)
			}else if(oper=="del"){
				var index = json-1;
				
				param.unOpenArr.splice(index,1);
				$.open_table_oper.param.unOpenArr.splice(index,1);
				
				param.ct_brzdArr.splice(index,1);
				$.open_table_oper.param.ct_brzdArr.splice(index,1);
				
				param.ktxfArr.splice(index,1);
				$.open_table_oper.param.ktxfArr.splice(index,1);
				
				param.ktjsArr.splice(index,1);
				$.open_table_oper.param.ktjsArr.splice(index,1);
			
				param.depositArr.splice(index,1);
				$.open_table_oper.param.depositArr.splice(index,1);

			}else if(oper=="edit"){
				
				param.unOpenArr[index-1] = json;
				$.open_table_oper.param.unOpenArr[index-1] = json;
				
				param.ct_brzdArr.push((json.th||"").toString());
				$.open_table_oper.param.ct_brzdArr.push((json.th||"").toString());
				
				param.ktxfArr[index-1] = (json.ktxf||"").toString();
				$.open_table_oper.param.ktxfArr[index-1] = ((json.ktxf||"").toString());
				
				param.ktjsArr[index-1] = ((json.ktjs||"").toString());
				$.open_table_oper.param.ktjsArr[index-1] = ((json.ktjs||"").toString());
				
				var depositJson = {}
				depositJson[(json.dcode).toString()] = (json.deposit||"").toString();
				param.depositArr[index-1] = depositJson;
				$.open_table_oper.param.depositArr[index-1] = depositJson;
			}
			console.log(JSON.stringify(param));
			//统计合计
			t.amountTotal();
		},
		amountTotal : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			/** 手牌数量统计  */
			//手牌总数量
			el.find(".num-item-total").text(param.unOpenArr.length);
			//男手牌总数量
			var filter = "select * from json where (cwlx_c=='男')";
    		var len = jsonsql.query(filter,param.unOpenArr).length;
    		el.find(".num-item-m").text(len);
			//女手牌总数量
    		var filter = "select * from json where (cwlx_c=='女')";
    		var len = jsonsql.query(filter,param.unOpenArr).length;
    		el.find(".num-item-w").text(len);
			/** 金额统计  */
    		var ktxfTotal = 0;
    		var depositTotal = 0;
    		var moneyTotal = 0;
    		$(param.unOpenArr).each(function(k,v){
    			ktxfTotal = ktxfTotal + parseFloat(v.xf_dj||0);
				depositTotal = depositTotal + parseFloat(v.deposit||0);
    		})
    		moneyTotal = ktxfTotal + depositTotal;
			//总金额
    		el.find(".money-item-ktxf").text(t.checkTwoPoint(ktxfTotal));
			//押金总金额
			el.find(".money-item-deposit").text(t.checkTwoPoint(depositTotal));
			//消费总金额
			el.find(".money-item-total").text(t.checkTwoPoint(moneyTotal));
		},
    	checkTwoPoint:function(value){//俩位小数校验
    		value=value.toString()+".00";
//    		value = value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
//    		value = value.replace(/^\./g,""); //验证第一个字符是数字
    		value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
    		value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    		value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
    		return value;
    	},
    	setJdInfo : function(ds){
    		var el = this.el;
    		var param = this.param;
    		var t = this;
    		var js = {jdh:"",company:"",pno:"",market:"",remark:""};
    		param.jdInfo = ds;
    		for(key in js){
    			el.find("#"+key).text(ds[key]||"");
    		}
    	}
	}

})(jQuery);